package com.tricycle.scada.mqtt.controller;

import com.tricycle.scada.mqtt.service.RealtimeValueService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@RequestMapping("test")
@RestController
public class RealtimeTestController {

    private RealtimeValueService realtimeValueService;

    public RealtimeTestController(RealtimeValueService realtimeValueService) {
        this.realtimeValueService = realtimeValueService;
    }

    @GetMapping("value")
    public Map<String, Set<String>> getValue(@RequestParam("tag") Set<String> tags,
                                @RequestParam(value = "start", required = false) Double start, @RequestParam(value = "end", required = false) Double end) {

        Map<String, CompletableFuture<Set<String>>> futuresMap = tags.stream()
                .collect(Collectors.toMap(
                        tag -> tag,  // key 是 tag
                        tag -> realtimeValueService.getValues(tag, start, end)  // value 是 CompletableFuture<Set<String>>
                ));

        return futuresMap.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,  // key 是 tag
                        entry -> entry.getValue().join()  // value 是 CompletableFuture 完成后的结果
                ));
    }
}
